/**
 * Represents one rule for firewall.
 */
package logic.pathelements;

import java.net.InetAddress;
/**
 * @author Dmitry Kuznetsov <zendipy@gmail.com>
 * @version 1.0
 * 
 */
public class FirewallRule {
	// port of source node
	private int srcPort;
	// port of destination node
	private int destPort;
	// ip-address of source node
	private InetAddress srcAddr;
	// ip-address of destination node
	private InetAddress destAddr;
	// action for this rule (1 - accept, 0 - deny)
	private byte action;
	// name of protocol type 
	private Protocol protocolType;
	// user friendly rule name
	private String name;
	
	/**
	 * Class constructor. Fill up rule's fields.
	 * @param name		user friendly rule name
	 * @param p 		protocol type
	 * @param srcPort	source port number
	 * @param destPort	destination port number
	 * @param srcIp		ip-address of source node
	 * @param destIp	ip-address of destination node
	 * @param action	action (1 - accept, 0 - deny)
	 */
	public FirewallRule(String name, Protocol protocol, int srcPort, 
			int destPort, InetAddress srcIp,InetAddress destIp, byte action)
	{
		this.name = name;
		this.protocolType = protocol;
		this.srcPort = srcPort;
		this.destPort = destPort;
		this.srcAddr = srcIp;
		this.destAddr = destIp;
		this.action = action;
	}
	
	public int getSrcPort() {
		return srcPort;
	}

	public int getDestPort() {
		return destPort;
	}

	public InetAddress getSrcAddr() {
		return srcAddr;
	}

	public InetAddress getDestAddr() {
		return destAddr;
	}

	public int getAction() {
		return action;
	}

	public Protocol getProtocolType() {
		return protocolType;
	}
	
	public String getName() {
		return name;
	}

	/**
	 * Convert internal action flag to string.
	 * @param act 	action in byte format
	 * @return 		human friendly string
	 */
	private String actionToString(byte act){
		if(act == 1 )
			return "ACCEPT";
		else
			return "DENY";
		
	}
	
	/**
	 * Create user friendly string from
	 * rule's fields
	 * @return string with information of this rule
	 */
	public String toString(){
		String result = this.protocolType.toString()+" "+
						srcAddr.toString() + ":" + srcPort + " " +
						destAddr.toString() + ":" + destPort + " "+
						actionToString(action);
		return result;
	}
}
